package dockerdriver

import (
	corev1 "gluenet/internal/agent/api/core/v1"
	"gluenet/internal/agent/depend"
	"gluenet/internal/agent/driver"
	"gluenet/internal/agent/driver/dockerdriver/fsouza"

	"github.com/spf13/viper"
)

type DockerDriver interface {
	Create(container *corev1.Container) (corev1.ContaienrDetial, error)

	Get(id string) (corev1.ContaienrDetial, error)

	Delete(id string) error

	Update(id string, option corev1.ContainerOptions) error

	Pull(image string) error

	Stop(containerId string) error

	Resume(containerId string) error

	ReStart(containerId string) error

	Remove(containerId string) error

	Exec(containerId string, cmd []string) error

	Close() error
}

func init() {
	depend.Provide(NewDockerConfig)
	depend.Provide(NewDockerDriver)
}

const (
	keyDockerConfig = driver.KeyDriverPerfix + `docker`
)

type DockerConfig struct {
	DockerAddress string `mapstructure:"docker_address"`
}

func NewDockerConfig(vp *viper.Viper) (*DockerConfig, error) {
	cfg := new(DockerConfig)
	if err := vp.UnmarshalKey(keyDockerConfig, cfg); err != nil {
		return nil, err
	}
	return cfg, nil
}

func NewDockerDriver(cfg *DockerConfig) (DockerDriver, error) {
	return fsouza.NewDriver(cfg.DockerAddress)
}
